<?php

/*
* Project: oCoder Framework
* File: /controllers/subject.php
* Purpose: controller for the subject of the app.
* Author: Trung Hoang
*/

class SubjectController extends BaseController
{

    //add to the parent constructor
    public function __construct($action, $urlValues)
    {
        //Check login
        if (!Helper::isLogin()) {
            //save current url: after login, auto redirect client to current url
            $_SESSION['lastUrl'] = Helper::currentPageURL();
            //redirect to login page
            header('Location: ' . BASE_URL . 'account/login');
        }

        parent::__construct($action, $urlValues);

        //create the model object
        require ("models/subject.php");
        $this->model = new SubjectModel();
    }

    //default action
    protected function index()
    {
        $this->view->output($this->model->index());
    }

    //create action
    protected function create()
    {
        //load data post from client
        if (!empty($_POST)) {
            if (!isset($_POST['keyword']) || $_POST['keyword'] == "")
                $_POST['keyword'] = $_POST['subject_name'];
            //save item and return id of item created
            $result = $this->model->create($_POST);
            if ($result) {
                $alert = array('class' => 'alert-success', 'message' => 'Add new user success');
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'subject/index');
            }
        }
        //load form create
        $this->view->output($this->model->create(''));
    }

    //update action
    protected function update()
    {
        //load data update post from client
        if (!empty($_POST)) {

            //get data from POST
            $subject_id = $_POST['subject_id'];
            foreach ($_POST as $col => $value) {
                $data[$col] = $value;
            }
            unset($data['subject_id']);
            //update item -> return total rows affected
            $result = $this->model->edit($subject_id, $data);
            echo $result;
            if ($result) {
                $alert = array('class' => 'alert-success', 'message' =>
                        'Update course successful');
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'Subject/index');
            }
        }
        //load form update
        $this->view->output($this->model->edit($subject_id = $this->urlValues['id'], $data =
            ''));
    }

    //get subject for schedule
    public function getlist()
    {
        $this->view->output($this->model->getList($this->urlValues['id']), "empty");
    }

    //delete action
    protected function delete()
    {
        //when client delete multi item
        if (!empty($_POST)) {
            $result = $this->model->delete('subject', 'subject_id IN(' . implode(',', $_POST['subject_id']) .
                ')');
        } else {
            $result = $this->model->delete('subject', 'subject_id=' . $this->urlValues['id']);
        }

        if ($result) {
            $alert = array('class' => 'alert-success', 'message' => 'Delete Subject success');
            $_SESSION['message'] = $alert;
        }

        header('Location: ' . BASE_URL . 'Subject/index');
    }

    public function search()
    {
        $course_id = intval($_POST['course_id']);
        $query = "";
                if($_POST['course_id'] == -1){
            $where="";
         }else{
            $where = " WHERE subject.courses_id = '{$course_id}'";
         }
        if (isset($_POST)) {
            
            $query .= "SELECT subject.*, courses.courses_name from subject LEFT JOIN courses ON (courses.courses_id = subject.courses_id)";
            $query .= $where;
            $query .= ' ORDER BY subject.subject_id DESC';
            $data = $this->model->selectMulti($query);

        } else {
            $data = array('error' => true);
        }
        header('Content-type: application/json');
        echo json_encode($data->data);
        exit();
    }

}

?>